#!/usr/bin/env python3
"""
关于页面路由
获取系统和应用程序信息
"""

from flask import Blueprint, jsonify
from flask_jwt_extended import jwt_required
import platform
import psutil
import os
from datetime import datetime

bp = Blueprint('about', __name__)


@bp.route('/system-info', methods=['GET'])
@jwt_required()
def get_system_info():
    """获取系统信息"""
    try:
        # 获取系统基本信息
        system_info = {
            'system': platform.system(),
            'node_name': platform.node(),
            'release': platform.release(),
            'version': platform.version(),
            'machine': platform.machine(),
            'processor': platform.processor(),
        }
        
        # 获取CPU信息
        cpu_info = {
            'physical_cores': psutil.cpu_count(logical=False),
            'logical_cores': psutil.cpu_count(logical=True),
            'cpu_percent': psutil.cpu_percent(interval=1),
            'cpu_freq': psutil.cpu_freq()._asdict() if psutil.cpu_freq() else None,
        }
        
        # 获取内存信息
        memory = psutil.virtual_memory()
        memory_info = {
            'total': memory.total,
            'available': memory.available,
            'used': memory.used,
            'percent': memory.percent,
        }
        
        # 获取磁盘信息
        disk = psutil.disk_usage('/')
        disk_info = {
            'total': disk.total,
            'used': disk.used,
            'free': disk.free,
            'percent': disk.percent,
        }
        
        # 获取Python信息
        python_info = {
            'version': platform.python_version(),
            'implementation': platform.python_implementation(),
        }
        
        # 获取应用信息
        app_info = {
            'name': 'Feng Admin',
            'version': '1.0.0',
            'description': '动态微应用管理框架',
            'uptime': _get_uptime(),
        }
        
        return jsonify({
            'success': True,
            'data': {
                'system': system_info,
                'cpu': cpu_info,
                'memory': memory_info,
                'disk': disk_info,
                'python': python_info,
                'app': app_info,
            }
        }), 200
        
    except Exception as e:
        return jsonify({
            'success': False,
            'error': str(e)
        }), 500


@bp.route('/app-info', methods=['GET'])
def get_app_info():
    """获取应用程序基本信息（不需要认证）"""
    return jsonify({
        'success': True,
        'data': {
            'name': 'Feng Admin',
            'version': '1.0.0',
            'description': '动态微应用管理框架',
            'features': [
                '用户认证系统',
                '待办事项管理',
                'Markdown 笔记',
                'JSON 格式化工具',
                'GitHub 热门仓库',
                'AI Playground',
                '智能体管理',
            ],
            'tech_stack': {
                'frontend': ['React', 'TypeScript', 'Vite', 'Tailwind CSS', 'shadcn/ui'],
                'backend': ['Python', 'Flask', 'SQLAlchemy', 'JWT'],
            }
        }
    }), 200


def _get_uptime():
    """获取系统运行时间"""
    try:
        boot_time = datetime.fromtimestamp(psutil.boot_time())
        uptime = datetime.now() - boot_time
        days = uptime.days
        hours = uptime.seconds // 3600
        minutes = (uptime.seconds % 3600) // 60
        return f"{days}天 {hours}小时 {minutes}分钟"
    except:
        return "未知"

